package cn.valorin;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.FurnaceRecipe;
import org.bukkit.plugin.java.JavaPlugin;

import cn.valorin.command.CommandsHandler;
import cn.valorin.configuration.Configuration;
import cn.valorin.configuration.languagefile.LanguageFileLoader;
import cn.valorin.data.MySQL;
import cn.valorin.event.CookEvent;
import cn.valorin.event.EatEvent;
import cn.valorin.event.Gui0Event;
import cn.valorin.event.Gui1Event;
import cn.valorin.event.HarvestEvent;
import cn.valorin.event.LeafEvent;
import cn.valorin.event.LineEvent;
import cn.valorin.event.PlantEvent;
import cn.valorin.event.SeedGainEvent;
import cn.valorin.item.Zongzi;
import cn.valorin.metric.Metrics;
import cn.valorin.via.Via;

public class Main extends JavaPlugin
{
    private static Main instance;
    private String prefix;
    private LanguageFileLoader lfl;
    private CommandsHandler ch;
    private MySQL mysql;
    
    public static Main getInstance() { return instance; }
    
    public String getPrefix() { return prefix; }
    
    public void setPrefix(String prefix) { this.prefix = prefix; }

	public LanguageFileLoader getLanguageFileLoader() { return lfl; }
	
	public void reloadLanguageFileLoader() {
		lfl.close();
		lfl = new LanguageFileLoader();
	}
	
	public CommandsHandler getCommandsHandler() { return ch; }
	
	public MySQL getMySQL() { return mysql; }
  
    @Override
    public void onEnable() {
  	    instance = this;
  	    lfl = new LanguageFileLoader();
  	  	getConfig().options().copyDefaults(true);
  	    saveDefaultConfig();
  	    saveConfig();
  	    Configuration.loadData();
  	    Configuration.saveData();
  	    Via.initial();
  	    
  	    prefix = getConfig().getString("Prefix").replace("&", "§");
  	    ch = new CommandsHandler("duanwu");
  	    reloadRecipe();
  	    
  	    mysql = new MySQL();
  	    mysql.connect();
  	  
  	    getServer().getPluginManager().registerEvents(new EatEvent(), this);
  	    getServer().getPluginManager().registerEvents(new SeedGainEvent(), this);
  	    getServer().getPluginManager().registerEvents(new Gui0Event(), this);
  	    getServer().getPluginManager().registerEvents(new Gui1Event(), this);
  	    getServer().getPluginManager().registerEvents(new CookEvent(), this);
  	    getServer().getPluginManager().registerEvents(new PlantEvent(), this);
  	    getServer().getPluginManager().registerEvents(new HarvestEvent(), this);
  	    getServer().getPluginManager().registerEvents(new LeafEvent(), this);
  	    getServer().getPluginManager().registerEvents(new LineEvent(), this);

  	  	Metrics metrics = new Metrics(this, 7951);
  	    metrics.addCustomChart(new Metrics.SimplePie("chart_id", () -> "My value"));
  	    metrics.addCustomChart(new Metrics.SingleLineChart("plant_times", () -> getData()));
  	    Bukkit.getConsoleSender().sendMessage("§b[端午节插件] §7加载完毕，作者Valorin，祝各位端午安康！");
    }

    @Override
    public void onDisable() {
	    Bukkit.getConsoleSender().sendMessage("§b[端午节插件] §7卸载完毕，作者Valorin");
	    if (mysql.isEnabled()) { mysql.close(); }
    }
    
    @SuppressWarnings("deprecation")
	public void reloadRecipe() {
		final FurnaceRecipe Furnace;   
		if (Material.getMaterial("INK_SACK") != null) {
            Furnace = new FurnaceRecipe(Zongzi.get(null), Via.ink_sack, 2);
		} else {
			Furnace = new FurnaceRecipe(Zongzi.get(null), Material.getMaterial("GREEN_DYE"));
		}
        getServer().addRecipe(Furnace);
    }

    int plantTimes = 0;
    public void addData() {
    	plantTimes++;
    }
    public int getData() {
    	int send = plantTimes;
    	plantTimes = 0;
    	return send;
    }
}
